package perfumecabinet

import (
	"context"
	"mall/common/globalkey"
	"mall/common/resultx"
	"mall/service/forum/model"
	"mall/service/forum/rpc/internal/svc"
	"mall/service/forum/rpc/types/forum"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreatePerfumeCabinetLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreatePerfumeCabinetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreatePerfumeCabinetLogic {
	return &CreatePerfumeCabinetLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// #24.11.07#  香水柜
func (l *CreatePerfumeCabinetLogic) CreatePerfumeCabinet(in *forum.PerfumeCabinetInfo) (*forum.BaseIDResp, error) {
	if in.UserId == nil || *in.UserId == 0 {
		return nil, resultx.StatusError(resultx.FORUM_PERFUME_CABINET_USER_ID_REQUIRED, "")
	}

	if in.ObjectId == nil || *in.ObjectId == 0 {
		return nil, resultx.StatusError(resultx.FORUM_PERFUME_CABINET_OBJECT_ID_REQUIRED, "")
	}

	if in.ObjectType == nil || *in.ObjectType == 0 {
		return nil, resultx.StatusError(resultx.FORUM_PERFUME_CABINET_OBJECT_TYPE_REQUIRED, "")
	}

	//首先查询userid 是否 拥有或者想要该款香氛
	filterMap := make(map[string]map[string][]any)
	filterMap[model.ForumUserPerfumeCabinetColumns.UserID] = map[string][]any{
		"equalTo": []any{*in.UserId},
	}
	filterMap[model.ForumUserPerfumeCabinetColumns.ObjectID] = map[string][]any{
		"equalTo": []any{*in.ObjectId},
	}
	if in.PlatformId != nil && *in.PlatformId >= 0 {
		filterMap[model.ForumUserPerfumeCabinetColumns.PlatformID] = map[string][]any{
			"equalTo": []any{*in.PlatformId},
		}
	}
	perfumeList, _, err := l.svcCtx.ForumPerfumeCabinet.GetList(l.ctx, model.M{}, -1, filterMap)
	if err != nil {
		return nil, err
	}

	now := time.Now()
	if len(perfumeList) > 0 {
		perfume := perfumeList[0]
		//如果香水已拥有  状态不显示改为显示   显示直接返回香水已拥有
		if int32(perfume.ObjectType) == globalkey.PerfumeCabinetTypeHave {
			if int32(perfume.Status) != globalkey.StatusEnabled {
				perfume.Status = uint8(globalkey.StatusEnabled)
			} else {
				return &forum.BaseIDResp{
					Id:  perfume.ID,
					Msg: "香水已拥有",
				}, nil
			}
		} else if int32(perfume.ObjectType) == globalkey.PerfumeCabinetTypeWantHave { //如果香水已想要  改状态 或者  改类型   显示直接返回香水已拥有
			if int32(*in.ObjectType) == globalkey.PerfumeCabinetTypeWantHave {
				if int32(perfume.Status) != globalkey.StatusEnabled {
					perfume.Status = uint8(globalkey.StatusEnabled)
				} else {
					return &forum.BaseIDResp{
						Id:  perfume.ID,
						Msg: "香水已想要",
					}, nil
				}
			} else if int32(*in.ObjectType) == globalkey.PerfumeCabinetTypeHave {
				perfume.ObjectType = uint8(globalkey.PerfumeCabinetTypeHave)
				perfume.Status = uint8(globalkey.StatusEnabled)
				perfume.UpdatedAt = now
			}
		}
		//修改 香水库
		err = l.svcCtx.ForumPerfumeCabinet.Update(l.ctx, nil, perfume)
		if err != nil {
			return nil, err
		}

		return &forum.BaseIDResp{
			Id: perfume.ID,
		}, nil
	}

	var perfume = &model.ForumUserPerfumeCabinet{
		UserID:     *in.UserId,
		PlatformID: uint(*in.PlatformId),
		ObjectID:   *in.ObjectId,
		ObjectType: uint8(*in.ObjectType),
		Status:     uint8(globalkey.StatusEnabled),
		CreatedAt:  now,
		UpdatedAt:  now,
	}
	err = l.svcCtx.ForumPerfumeCabinet.Insert(l.ctx, nil, perfume)
	if err != nil {
		return nil, err
	}

	return &forum.BaseIDResp{
		Id:  perfume.ID,
		Msg: "添加成功",
	}, nil
}
